package edu.spsu.cs4253;

import java.io.*;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;

public class Encryptor 
{	
	FileInputStream org;
	FileOutputStream encryptedOut;
	FileInputStream encryptedIn;
	FileOutputStream decryptedOut;
	String key;
	
	
	public Encryptor(File in, File encrypted, File decrypted, String k)
	{
		try
		{
			key = k;
			org = new FileInputStream(in);
			encryptedOut = new FileOutputStream(encrypted);
			encryptedIn = new FileInputStream(encrypted);
			decryptedOut = new FileOutputStream(decrypted);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	public void encrypt() throws Throwable {
		encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, org, encryptedOut);
	}

	public void decrypt() throws Throwable {
		encryptOrDecrypt(key, Cipher.DECRYPT_MODE, encryptedIn, decryptedOut);
	}

	public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {

		DESKeySpec dks = new DESKeySpec(key.getBytes());
		SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
		SecretKey desKey = skf.generateSecret(dks);
		Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE

		if (mode == Cipher.ENCRYPT_MODE) {
			cipher.init(Cipher.ENCRYPT_MODE, desKey);
			CipherInputStream cis = new CipherInputStream(is, cipher);
			doCopy(cis, os);
		} else if (mode == Cipher.DECRYPT_MODE) {
			cipher.init(Cipher.DECRYPT_MODE, desKey);
			CipherOutputStream cos = new CipherOutputStream(os, cipher);
			doCopy(is, cos);
		}
	}

	public static void doCopy(InputStream is, OutputStream os) throws IOException {
		byte[] bytes = new byte[64];
		int numBytes;
		while ((numBytes = is.read(bytes)) != -1) {
			os.write(bytes, 0, numBytes);
		}
		os.flush();
		os.close();
		is.close();
	}
}
